﻿@model IEnumerable<KVP.KVPNew<string, string>>
@{ 
    string Val = "", style = "", onchange = "", raisechange = "", onblur = "";
    bool raisechangeonstartup = true;
    if (ViewData.Values != null && ViewData.Values.Count > 0)
    {
        Val = (ViewData["selectedvalue"] ?? "").ToString();
        style = (ViewData["style"] ?? "").ToString();
        onchange = (ViewData["onchange"] ?? "").ToString();
        raisechange = (ViewData["raisechange"] ?? "").ToString();
        onblur = (ViewData["onblur"] ?? "").ToString();
       
        
    }
    if (!string.IsNullOrEmpty(raisechange))
    {//do change by default
        raisechangeonstartup = (raisechange == "1");
    }

    var id = ViewData.TemplateInfo.GetFullHtmlFieldId("");
   
    if (!string.IsNullOrEmpty(onchange))
    {
        onchange = string.Format(onchange, id);
        onchange = "onchange=\"javascript:" + onchange + "\"";

    }
    if (!string.IsNullOrEmpty(onblur))
    {
        onblur = string.Format(onblur, id);
        onblur = "onblur=\"javascript:" + onblur + "\"";

    }
    bool ExistValue = false;
}

<select id="@id" name='@ViewData.TemplateInfo.GetFullHtmlFieldName("")' style="@style" @Html.Raw(onchange)  @Html.Raw(onblur) >
    @foreach (var val in Model)
    {
        if (!ExistValue && val.Key == Val)
        {
            ExistValue = true;
        }
        string selected = (val.Key == Val) ? "selected=selected" : "";
           
        <option  value='@val.Key' @selected >@val.Value </option>
    }
</select>
@if (raisechangeonstartup)
{ 
    <script type="text/javascript">
         
           $(document).ready(function () {
           var id= '#@id';
               
               @if (ExistValue)
               { 
       
       <text>$(id).change();</text>
    
               }
               else
               {
    
    <text>    
        $(id).attr('selectedIndex', -1);
    </text>    
          
    
               }
    
    
           });

   
           
    </script>
}
